package com.example.fairplay.newfpregistration.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.fairplay.newfpregistration.R;
import com.example.fairplay.newfpregistration.adapter.FileAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;



public class FilePickActivity extends AppCompatActivity {
    public static final int PICK_FILE_RESULT_CODE = 0;// 结果码
    private String rootPath;// 根路径
    private String currentPath;// 当前路径
    private ListView lvFiles;
    private List<File> files;// 文件列表
    private FileAdapter fileAdapter;// 文件适配器
    private ItemClickListener itemClickListener;
    private File currentFile;// 当前选中的文件
    private TextView tvEmpty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_pick);
        initWidget();
        initData();
    }

    /**
     * 初始化控件
     */
    private void initWidget() {
        tvEmpty = (TextView) findViewById(R.id.tv_empty);
        lvFiles = (ListView) findViewById(R.id.lv_files);
        lvFiles.setEmptyView(tvEmpty);
        itemClickListener = new ItemClickListener();
        lvFiles.setOnItemClickListener(itemClickListener);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        files = new ArrayList<File>();
        fileAdapter = new FileAdapter(FilePickActivity.this, files);
        lvFiles.setAdapter(fileAdapter);
        rootPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        updateFiles(rootPath);
    }


    /**
     * 设置listview 适配器
     * @param path
     */
    private void updateFiles(String path) {
        currentPath = path;
        File file = new File(path);
        if (file.exists()) {
            if (file.isDirectory()) {
                // path路径的文件为目录时,添加子文件
                files.clear();
                File[] childFiles = file.listFiles();
                for (File tempFile : childFiles) {
                    if (!tempFile.isHidden()) {
                        files.add(tempFile);
                    }
                }
            } else {
                // path路径的文件为非目录时
                files.clear();
                files.add(file);
            }
            fileAdapter.notifyDataSetChanged();
        }
    }


    /**
     * 返回键监听
     */
    @Override
    public void onBackPressed() {
        // 当前路径为根路径时退出，否则返回上一层
        if (currentPath.equals(rootPath)) {
            Intent intentData = new Intent();
            intentData.putExtra("filePath", "");
            setResult(0, intentData);
            super.onBackPressed();
        } else {
            File curFile = new File(currentPath);
            updateFiles(curFile.getParent());
        }
    }

    /**
     * listview item点击事件
     *
     * @author fanxi
     *
     */
    private class ItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view,
                                int position, long id) {
            currentFile = files.get(position);
            if (!currentFile.isDirectory()) {
                // 选中的file不是目录时返回选择结果
                Intent intentData = new Intent();
                intentData.putExtra("filePath", currentFile.getAbsolutePath());
                setResult(PICK_FILE_RESULT_CODE, intentData);
                finish();
            } else {
                // 点击的file是目录时则进入目录
                updateFiles(currentFile.getAbsolutePath());
            }
        }

    }
}
